package com.clps.algorithm03.链表;

public class Queue2<E>{
    private final Object[] data; // 队列中存放元素的数组
    private final int maxSize;// 队列的最大容量
    private int size; // 队列中元素的个数
    private int head = 0;// 队列的头，只能删除，指向队列的第一个元素
    private int tail = 0; // 队列的尾，只能添加，指向队列最后一个元素的下一个位置

    public Queue2(Object[] data, int maxSize) {
        this.data = data;
        this.maxSize = maxSize;
    }


    public void add(E val){
        if(isFull()){
            throw  new IllegalStateException("队列已经满了,无法加入");
        }
        data[tail++] = val;
        size++;
    }

    public  E top(){
        if(isEmpty()){
            throw  new IllegalStateException("队列为空");
        }
        return (E) data[head];
    }

    private boolean isEmpty() {
        return size ==0;
    }

    private boolean isFull() {
        return  tail == maxSize;
    }
    public  int size(){
        return  size;
    }
}
